package geektime.spring.springbucks.service.impl;

import com.github.pagehelper.PageHelper;
import geektime.spring.springbucks.entity.TCoffee;
import geektime.spring.springbucks.dao.TCoffeeDao;
import geektime.spring.springbucks.service.TCoffeeService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (TCoffee)表服务实现类
 *
 * @since 2022-10-30 00:14:40
 */
@Service("tCoffeeService")
@CacheConfig(cacheNames = "coffee")
public class TCoffeeServiceImpl implements TCoffeeService {
    @Resource
    private TCoffeeDao tCoffeeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @Cacheable(key = "#id")
    public TCoffee queryById(Long id) {
        return this.tCoffeeDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<TCoffee> queryByPage(Map<String, String> args, int pageNum, int pageSize) {
        if (pageNum == 0) pageNum = 1;
        if (pageSize <= 0) pageSize = 15;
        PageHelper.startPage(pageNum, pageSize);
        return this.tCoffeeDao.queryAllByLimit(args);
    }

    /**
     * 新增数据
     *
     * @param tCoffee 实例对象
     * @return 实例对象
     */
    @Override
    public TCoffee insert(TCoffee tCoffee) {
        this.tCoffeeDao.insert(tCoffee);
        return tCoffee;
    }

    /**
     * 修改数据
     *
     * @param tCoffee 实例对象
     * @return 实例对象
     */
    @Override
    public TCoffee update(TCoffee tCoffee) {
        this.tCoffeeDao.update(tCoffee);
        return this.queryById(tCoffee.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.tCoffeeDao.deleteById(id) > 0;
    }
}
